/* main.c */
#include "app_inc.h"

volatile bool bTimeoutFlag;

void LPTMRx_Configuration(void);

int main(void)
{
    BSP_InitSystem();
    BSP_InitStdioLPUART(115200U);

    printf("\r\n\r\nLPTMR_Timer Example.\r\n");
    printf("Compiled on %s, %s\r\n\r\n", __DATE__, __TIME__);

    LPTMRx_Configuration();
    
    LPTMR_HAL_Start();

    while (1)
    {
        bTimeoutFlag = false;
        while (!bTimeoutFlag)
        {}
        printf("Timer Timeout.\r\n");
    }
}

void LPTMRx_Configuration(void)
{
    LPTMR_PitConfig_T lptmrPitConfigStruct;

    /* 配置LPTMR0时钟源 */
    PCC->CLKCFG[PCC_LPTMR0_INDEX] = PCC_CLKCFG_CGC_MASK | PCC_CLKCFG_PCS(6); /* BSP_CLK_SPLLDIV2_HZ, 84MHz. */
    
    /* 配置定时器 */
    LPTMR_HAL_Reset();
    
    lptmrPitConfigStruct.ClockSource = eLPTMR_ClockSource_PCC_Sel; /* 来自PCC的时钟源 */
    lptmrPitConfigStruct.ClockPreDiv = eLPTMR_ClockPreDiv_65536; /* 尽量大的分频 */
    LPTMR_HAL_ConfigPitMode(&lptmrPitConfigStruct);
    
    LPTMR_HAL_SetTimeoutValue(BSP_CLK_SPLLDIV2_HZ >> 16); /* 使用eLPTMR_ClockPreDiv_65536分频后的1s */
    
    /* 开中断 */
    NVIC_EnableIRQ(LPTMR0_IRQn);
    LPTMR_HAL_SetIntEnabledOnTimeout(true);
}

/* LPTMR0中断服务程序入口 */
void LPTMR0_IRQHandler(void)
{
    LPTMR_HAL_ClearTimeoutFlag();
    
    bTimeoutFlag = true;
}

/* EOF. */

